<?php
$isReloadable = false;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');
require_once('../../_inc/funct.tabDati.php');

$aCamForm = array();
require("_inc/const.campiModRglob.php");

if (reqVar('azione') == 'salva') {
	$msgPag = confMod($aCamForm);
} 
if (reqVar('azione') == '') {
	$sqlSel = "SELECT * FROM rgl_mod_rglob " .
		" WHERE id_mod_rglob = ".intval(reqVar('id'));
	$objDB->exec($sqlSel,'modRec');
	if (!$aRow = $objDB->fetch('modRec')) {
		backOnErr("mod_rglob - id. '".reqVar('id')."' mancante o errato");
	}
	foreach($aCamForm as $rX => $aRX) {
		foreach($aRX as $jX => $aCX) {
			$_REQUEST[$aCX[0]] = $aRow[$aCX[0]];
		}
	}
	$aPuntMbil = array(20=>'C',30=>'CC',40=>'CCC',50=>'B',60=>'BB',70=>'BBB',80=>'A',90=>'AA',100=>'AAA');
	$sSql = "SELECT p.*  FROM rgl_puntglob p WHERE id_mod_rglob = ".reqVar('id')
		." ORDER BY p.punteggio ";
	$objDB->query($sSql,'tdatiInp');
	while($aP=$objDB->fetch('tdatiInp')) {
		$pX = $aP['punteggio'];
		reqDef('pglo_sigla_'.$pX,$aP['sigla']);
		reqDef('pglo_desbr_'.$pX,$aP['descr']);
		reqDef('pglo_deses_'.$pX,$aP['descr_estesa']);
		reqDef('pglo_color_'.$pX,$aP['colore']);	
	}
	foreach($aPuntMbil as $pX => $cX) {
		reqDef('pglo_sigla_'.$pX,$cX);
		reqDef('pglo_desbr_'.$pX,'');
		reqDef('pglo_deses_'.$pX,'');
		reqDef('pglo_color_'.$pX,'rosso');		
	}
}
testaHtml('Gestione modelli',$msgPag,'modl');
?>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
         &nbsp;&nbsp;&nbsp;
         <a class="cmd1" href="javascript:document.frm1.submit();" >salva</a>
			<? echo cmdTorna('torna'); ?>
      </td>
   </tr>
</table>
<?=getHtmlForm('frm1','',array('enctype'=>true));?>
<?=getHtmlHidden('id');?>
<?=getHtmlHidden('azione','salva');?>
<?=getHtmlHidden('xsn',XSN);?>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<?=getHtmlRigaRif();?>
   <tr>
      <td class="lab1" colspan='20' align='center'>Definizione tabella dati</td>
   </tr>
<?
echo getHtmlCampiForm($aCamForm);
?>
   <tr>
      <td class="lab1" colspan='20' align='center'>Punteggi</td>
   </tr>
   <tr>
      <td class="lab1" colspan='2' align='center'>Soglia</td>
      <td class="lab1" colspan='2' align='center'>Sigla</td>
      <td class="lab1" colspan='12' align='center'>Descrizione breve / estesa</td>
      <td class="lab1" colspan='4' align='center'>colore</td>
   </tr>
<?
$aColorPunt = array('rosso'=>'rosso','giallo'=>'giallo','verde'=>'verde');
foreach($_REQUEST as $nX => $vX) {
	if (substr($nX,0,11)!='pglo_sigla_') continue;
	$pX = substr($nX,11);
?>
   <tr>
      <td class="txt1" colspan='2' align='right' valign='top'><?=$pX?>&nbsp;</td>
      <td class="txt1" colspan='2' align='center' valign='top'><?=getHtmlText('pglo_sigla_'.$pX,3,3)?></td>
      <td class="txt1" colspan='12' align='left' valign='top'>
      	&nbsp;&nbsp;&nbsp;<?=getHtmlText('pglo_desbr_'.$pX,80,80)?><br/>
      	&nbsp;&nbsp;&nbsp;<?=getHtmlTextArea('pglo_deses_'.$pX,80,2)?>
      </td>
      <td class="txt1" colspan='4' align='left' valign='top'>
      	&nbsp;&nbsp;&nbsp;<?=getHtmlSelect('pglo_color_'.$pX,$aColorPunt)?>
      </td>
   </tr>
<?
}
?>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
         &nbsp;&nbsp;&nbsp;
         <a class="cmd1" href="javascript:document.frm1.submit();" >salva</a>
			<? echo cmdTorna('torna'); ?>
      </td>
   </tr>
</table>
			<?
echo "</form>\n";
codaHtml();
exit();

function confMod($aCamForm) {
	global $objDB;
	$msg = '';
	$_REQUEST['cod_mod_rglob'] = strtoupper(trim(reqVar('cod_mod_rglob')));
	if (reqVar('cod_mod_rglob')=='') {
		$msg .= _t("Codice obbligatorio")."<br>";
	}
	if (preg_match("/[^_A-Z0-9]/",reqVar('cod_mod_rglob'))) {
		$msg = _t("Codice '%s' puo' contenere solo lettere numeri e '-'",reqVar('cod_mod_rglob'))."<br>";
	}
	if (trim(reqVar('descr'))=='') {
		$msg .= "Descrizione obbligatoria<br>";
	}
	$sqlSel = "SELECT id_mod_rglob FROM rgl_mod_rglob " 
			." WHERE cod_mod_rglob = '".reqVarAS('cod_mod_rglob')."'"
			." AND id_mod_rglob <> ".reqVar('id');
	$objDB->exec($sqlSel);
	$aRow = $objDB->fetch();
	if ($aRow) {
		$msg .= _t("Codice gia' esistente")."<br>";
	}
		if ($msg != '') {
		return $msg;
	}
			
	$sSql = "UPDATE rgl_mod_rglob SET cod_mod_rglob = '".trim(reqVarAS('cod_mod_rglob'))."'";
	foreach($aCamForm as $rX => $aRX) {
		foreach($aRX as $jX => $aCX) {
			if ($aCX[0]=='cod_mod_rglob') continue;
			$sSql .= ", ".$aCX[0]." = '".reqVarAS($aCX[0])."' ";
		}
	}
	$sSql .= " WHERE id_mod_rglob = ".reqVarInt('id');
	$objDB->exec($sSql);
	//giudizi
	$sSql = "DELETE FROM rgl_puntglob WHERE id_mod_rglob = ".reqVarInt('id');
	$objDB->exec($sSql);
	foreach($_REQUEST as $nX => $vX) {
		if (substr($nX,0,11)!='pglo_sigla_') continue;
		$pX = substr($nX,11);
		$sSql = "INSERT INTO rgl_puntglob SET id_mod_rglob = ".reqVarInt('id')
			.", punteggio = ".$pX
			.", sigla = '".reqVarAS('pglo_sigla_'.$pX)."' "
			.", descr = '".reqVarAS('pglo_desbr_'.$pX)."' "
			.", descr_estesa = '".reqVarAS('pglo_deses_'.$pX)."' "
			.", colore = '".reqVarAS('pglo_color_'.$pX)."' "
			;
		$objDB->exec($sSql);
	}	
	header("Location: ".urlTorna("Modello '".reqVar('cod_mod_rglob')."' modificato"));
	exit();
}
